package com.ls.lserp.modules.sys.utils;

import com.google.common.collect.Maps;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.Constants;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.dbaccess.Entity.DesignOptionPEntity;
import com.ls.lserp.dbaccess.Entity.OperatingPEntity;
import com.ls.lserp.dbaccess.Entity.OperatingREntity;
import com.ls.lserp.modules.sys.dao.OperatingDao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


/**
 * 工序项目工具类
 *
 * @author T.S.Y
 * @version 2018-06-28
 */
public class OperatingUtils {

	private static OperatingDao operatingDao = SpringContextHolder.getBean(OperatingDao.class);
	//工序基础数据key
	public static final String CACHE_OPERATING_VALUE_MAP = "operatingValueMap";
	//工序设计项目数据集key
	public static final String CACHE_OPERATING_DESIGN_MAP = "operatingDesignMap";

	/**
	 * 放入缓存初始方法
	 */
	public static void putOperatingCache() {
		putDesignListByOpIdCache();
		putOperatingEntityCache();
	}
	
	/**
	 * 清除缓存方法
	 */
	public static void removeOperatingCache() {
		CacheUtils.remove(CACHE_OPERATING_VALUE_MAP);
		CacheUtils.remove(CACHE_OPERATING_DESIGN_MAP);
	}
	
	/**
	 * 按照不同工序将设计项目数据集放入缓存中
	 * @return
	 */
	public static Map<Long, List<DesignOptionPEntity>> putDesignListByOpIdCache() {
		@SuppressWarnings("unchecked")
		Map<Long, List<DesignOptionPEntity>> designListMap = (Map<Long, List<DesignOptionPEntity>>) CacheUtils.get(CACHE_OPERATING_DESIGN_MAP);
		if (designListMap == null) {
			designListMap = Maps.newHashMap();
			for (OperatingPEntity opEntity : findAllList(new OperatingPEntity())) {
				designListMap.put(opEntity.getItemId(), getDesignListByOpId(opEntity.getItemId()));
			}
			CacheUtils.put(CACHE_OPERATING_DESIGN_MAP, designListMap);
		}
		return designListMap;
	}
	
	/**
	 * 将工序基础数据放入缓存中
	 * @return
	 */
	public static Map<Long, OperatingPEntity> putOperatingEntityCache() {
		@SuppressWarnings("unchecked")
		Map<Long, OperatingPEntity> operatingMap = (Map<Long, OperatingPEntity>) CacheUtils.get(CACHE_OPERATING_VALUE_MAP);
		if (operatingMap == null) {
			operatingMap = Maps.newHashMap();
			for (OperatingPEntity opEntity : findAllList(new OperatingPEntity())) {
				operatingMap.put(opEntity.getItemId(), opEntity);
			}
			CacheUtils.put(CACHE_OPERATING_VALUE_MAP, operatingMap);
		}
		return operatingMap;
	}
	
	/**
	 * 获取全部工序基础数据
	 * @param operatingPEntity
	 * @return
	 */
	public static List<OperatingPEntity> findAllList(OperatingPEntity operatingPEntity) {
		return operatingDao.findAllList(operatingPEntity);
	}
	
	/**
	 * 获取设计基础数据
	 * @return
	 */
	public static List<DesignOptionPEntity> findBasicDesignList() {
		return operatingDao.findBasicDesignList();
	}
	
	/**
	 * 获取量体基础数据
	 * @return
	 */
	public static List<DesignOptionPEntity> findBasicFitList() {
		return operatingDao.findBasicFitList();
	}
	
	/**
	 * 按工序id获取工序设计项目关联表数据
	 * @param operating
	 * @return
	 */
	public static List<OperatingREntity> findOpFitList(Long operating) {
		return operatingDao.findOpFitList(operating);
	}
	
	/**
	 * 按工序id获取工序量体项目关联表数据
	 * @param operating
	 * @return
	 */
	public static List<OperatingREntity> findOpDesignList(Long operating) {
		return operatingDao.findOpDesignList(operating);
	}
	
	/**
	 * 按工序id获取设计项目和量体数据集，并排序
	 * @param operatingId
	 * @return
	 */
	public static List<DesignOptionPEntity> getDesignListByOpId(Long operatingId) {
		// 结果集
		List<DesignOptionPEntity> qualityList = new ArrayList<DesignOptionPEntity>();
		// 设计基础数据
		List<DesignOptionPEntity> designBasicList = findBasicDesignList();
		// 量体基础数据
		List<DesignOptionPEntity> fitBasicList = findBasicFitList();
		// 工序设计项目关联表数据
		List<OperatingREntity> opDesignList = findOpDesignList(operatingId);
		// 工序量体项目关联表数据
		List<OperatingREntity> opFitList = findOpFitList(operatingId);
		// 遍历获取设计项目数据集
		for (OperatingREntity oEntity : opDesignList) {
			for (DesignOptionPEntity design : designBasicList) {
				if (oEntity.getDesignopionId().intValue() == design.getDesignId() ) {
					// 设置排序序号
					design.setItemSort(oEntity.getSort().intValue());
					// 添加到结果集中
					qualityList.add(design);
				}
			}
		}
		// 遍历获取量体项目数据集
		for (OperatingREntity oEntity : opFitList) {
			for (DesignOptionPEntity design : fitBasicList) {
				if (oEntity.getDesignopionId().intValue() == design.getDesignId() ) {
					// 设置排序序号
					design.setItemSort(oEntity.getSort().intValue());
					// 添加到结果集中
					qualityList.add(design);
				}
			}
		}
		// 排序
		// 对于Comparator接口的重写
		// 这个接口就一个抽象函数，给出的参数与返回值都是定死的。
		Collections.sort(qualityList, new Comparator<DesignOptionPEntity>() {
			@Override
			public int compare(DesignOptionPEntity o1, DesignOptionPEntity o2) {
				//首先设置要比较的东西
				int i = o1.getItemSort() - o2.getItemSort();
				// 这个返回值代表了被比较的那两个东西谁大谁小，1（正数）就是前面的大，0是一样，-1（负数）是后面的大
				return i;
			}		
		});
		// 返回结果集
		return qualityList;
	}
	
	/**
	 * 返回工序项目名称
	 * 
	 * @param value
	 * @return
	 */
	public static String getOperatingItemName(Long value) {
		// 获取缓存数据
		Map<Long, OperatingPEntity> opEntityMap = putOperatingEntityCache();
		// 通过key获取value
		OperatingPEntity returnOpEntity = opEntityMap.get(value);
		// 返回数据
		if (returnOpEntity != null) {
			if (Constants.EN_US.equals(StringUtils.getLanguage())) {
				return returnOpEntity.getItemNameEn();
			} else {
				return returnOpEntity.getItemNameCn();
			}
		} else {
			return "";
		}
	}
	
	/**
	 * 返回设计项目数据集
	 * 
	 * @param value
	 * @return
	 */
	public static List<DesignOptionPEntity> getDesignListByOpId(String operatingId) {
		// 获取缓存数据
		Map<Long, List<DesignOptionPEntity>> designListMap = putDesignListByOpIdCache();
		// 通过key获取value
		List<DesignOptionPEntity> returnDesignList = designListMap.get(operatingId);
		// 返回数据
		if (returnDesignList != null) {
			return returnDesignList;
		} else {
			return new ArrayList<DesignOptionPEntity>();
		}
	}
	
}
